<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sửa hóa đơn đặt hàng</title>
</head>

<body>
<?PHP
	require('models/m_phieuxuat.php');
	require ('models/sharefunction.php'); 
	dbconnect() ;
	$post=getbyid_hoadon($_GET['id']);
 	$nd= getbyid_nguoidung($post["ID_Nguoi_dung"]);
	$ctx=getbyid_ctx($_GET['id']);
	$sach = getbyall_sach();
	if(isset($_POST['xoa2']) and isset($_POST['data'])) 
	{
		$id_array = $_POST['data']; // return array
		$id_count = count($_POST['data']); // count array
			for($i=0; $i < $id_count; $i++) 
					{
					$id = $id_array[$i];
					$query = mysql_query("DELETE FROM Chi_tiet_hoa_don WHERE ID = '$id'");
					header("Refresh:0");
					if(!$query) { die(mysql_error()); }
					}
	
	}
	if(isset($_POST['update']) and isset($_POST['soluong']) and isset($_POST['id'])) 
	{
		$id_array_soluong = $_POST['soluong']; // return array
		$id_array=$_POST['id'];
		$id_count = count($_POST['soluong']); // count array
			for($i=0; $i < $id_count; $i++) 
					{
					$soluong = $id_array_soluong[$i];
					$idx = $id_array[$i];
					
					$query1 = mysql_query("SELECT * FROM chi_tiet_hoa_don WHERE ID ='$idx'");
					$row = mysql_fetch_array($query1);
					$thanhtien= $row['Gia_ban']*$soluong;
					$laisuat = $soluong *($row['Gia_ban'] -$row['Gia_nhap']);
					$query = mysql_query("UPDATE chi_tiet_hoa_don SET So_luong='$soluong', Thanh_tien='$thanhtien', Lai_suat='$laisuat' WHERE ID ='$idx'");
					
					
					if(!$query) { die(mysql_error()); }
					update_thongtinthem($_POST['thongtinthem'],$_GET['id']);
					header("Refresh:0");
					}
	
	}
	
	
	if(isset($_POST['them'])) 
	{
		$idsach= $_POST['ID_Sach'];
		$soluong = $_POST['So_luong_x'];
		$row= getbyid_sach($idsach);
		$row2=getbyid_giaban($idsach);
		$row3=getbyid_gianhap($idsach);
		$thanhtien=$soluong*$row2['Gia_ban'];
		$laisuat=($row2['Gia_ban']-$row3['Gia_nhap'])*$soluong;
		insert_cthd($idsach,$soluong,$row2['Gia_ban'],$row3['Gia_nhap'],$_GET['id'],$thanhtien,$laisuat);
		header("Refresh:0");
	}  

	require ('v_hoadon_sua.php'); 
	
?>
</body>
</html>